/**
  ETFRoot project, Anyang Normal University && IMP-CAS
  \class ETFHit
  \brief An abstract base class for a hit. A hit differs from a raw channel in that
  though hit contains the raw channel, it has the attributes of the channel, such
  as the postion and the calibration constants. And a hit always belongs to a detecor.
  \author SUN Yazhou, asia.rabbit@163.com
  \since 2021-10-28
  \date 2021-12-17 last modified
  \attention
  changelog
  <table>
  <tr>  <th>Date         <th>Author      <th>Description                   </tr>
  <tr>  <td>2021-10-28   <td>Asia Sun    <td>file created                  </tr>
  </table>

  \copyright Copyright (c) 2021 Anyang Normal U. && IMP-CAS with LGPLv3 LICENSE
*/

#include <cstring>
#include <iostream>
#include <TGraph.h>
#include "ETFHit.h"
#include "ETFMsg.h"

using std::cout;
using std::endl;

ClassImp(ETFHit);

ETFHit::ETFHit() : fDelay(0.), fGHit(nullptr){
  Initialize();
  fGHit = new TGraph(); fGHit->SetName(GetName());
} // end ctor

// copy ctor
ETFHit::ETFHit(const ETFHit &t){
  *this = t;
} // end copy ctor
// assignment ctor
ETFHit &ETFHit::operator=(const ETFHit &t){
  if(&t == this) return *this;

  fFiredStatus = t.fFiredStatus;
  memcpy(fPos, t.fPos, sizeof(fPos));

  return *this;
} // end member function operator=

ETFHit::~ETFHit(){
  if(fGHit) delete fGHit;
} // end dtor

void ETFHit::SetPosition(const double *p){
  memcpy(fPos, p, sizeof(fPos));
} // end member function SetPosition

void ETFHit::GetPosition(double *p) const{
  memcpy(p, fPos, sizeof(fPos));
} // end member function GetPosition

void ETFHit::Initialize(){
  fFiredStatus = -1;
  fPos[0] = -9999; fPos[1] = -9999; fPos[2] = -9999;
} // end member function Initialize

//configure fGHit
void ETFHit::ConfigureDraw(){
  if(!fGHit){
    fGHit = new TGraph();
    fGHit->SetName(GetName());
    fGHit->SetMarkerColor(kRed);
  } // end if
  while(fGHit->GetN()) fGHit->RemovePoint(0); // clear all the points
  fGHit->SetPoint(0, GetZ(), GetX());
} // end member function ConfigureDraw
// draw the hit
void ETFHit::Draw(){
  ConfigureDraw();
  fGHit->Draw("psame");
} // end member function Draw

// print the hit info
void ETFHit::Print(){
  cout << "\033[32;1m----------- HIT " << GetName();
  cout << " PRINT -----------\033[0m" << endl;
  cout << "Pos(mm): (x, y, z): ";
  cout << "(" << fPos[0] << ", " << fPos[1] << ", " << fPos[2] << ")" << endl;
  cout << "FiredStatus: " << fFiredStatus << " fDelay: " << fDelay << endl;
} // end member function Print
